//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.gx.rabbit.consumer;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Author Liu-pj
 * @Date 2023/3/9 17:40
 * @Desc 消息统一处理
 **/
public abstract class AbstractMessageListener implements ChannelAwareMessageListener {
    private static final Logger log = LoggerFactory.getLogger(AbstractMessageListener.class);

    public AbstractMessageListener() {
    }

    public void onMessage(Message message, Channel channel) {
        MessageProperties messageProperties = message.getMessageProperties();
        long deliveryTag = messageProperties.getDeliveryTag();
        String msgStr = new String(message.getBody(), StandardCharsets.UTF_8);
        log.info("发送消息，消息ID:{}, 消息内容:{}", messageProperties.getMessageId(), msgStr);
        try {
            this.processMessage(msgStr);
            channel.basicAck(deliveryTag, false);
            log.info("消息消费成功，确认消息ID:{}", messageProperties.getMessageId());
        } catch (Exception var11) {
            log.error("签收失败", var11);
            if (message.getMessageProperties().getRedelivered()) {
                try {
                    channel.basicReject(deliveryTag, false);
                } catch (IOException var10) {
                    log.error("拒签失败", var10);
                }
            } else {
                try {
                    channel.basicNack(deliveryTag, false, true);
                } catch (IOException var9) {
                    log.error("重新入队失败", var9);
                }
            }
        }

    }

    protected abstract void processMessage(String var1);

}
